--[[
* 属性增减  与43差不多, 不过增加了 最大值
*]]
---@class BuffEffect74 : BaseBuffEffect
BuffEffect74 = ClientFight.CreateClass("BuffEffect74", ClientFight.BaseBuffEffect)
local t = BuffEffect74
---@param fight Fight
---@param buff FighterBuff

function t:action(fight, buff, ...)
    local calculatorType = buff.buffBean.f_BuffEffectValue[1];
    local plus = buff.buffBean.f_BuffEffectValue[2];

    local calculator = nil;
    if (calculatorType == 0) then
        calculator = buff.source;
    elseif calculatorType == 1 then
        calculator = buff.target;
    elseif calculatorType == 2 then
        calculator = buff.skill.target;
    elseif calculatorType == 3 then
        calculator = buff.skill.parentSkill.target;

    end
    if (calculator == nil) then
        return IBuffEffect.FAIL;

    end
    local f_buffEffectValue = buff.buffBean.f_BuffEffectValue;
    local isSelf = calculator == buff.target;
    for i = 3, table.getn(f_buffEffectValue), 3 do
        if table.getn(f_buffEffectValue < i + 3) then
            break ;
        end
        local attrType = f_buffEffectValue[i];
        local attrRatio = f_buffEffectValue[i + 1];
        if (isSelf) then
            attrType = convSelf(attrType);
            buff.attributes.changeAttribute(attrType, attrRatio * plus);
            if (buff.attributes.attributes[attrType] > f_buffEffectValue[i + 2]) then
                buff.attributes.attributes[attrType] = 0 + f_buffEffectValue[i + 2];
            end
            local logType = attrType;
            --LogUtil.info(fight, ()-> "[" + fight.getFrame() + "][EFFECT]Fighter " + buff.target.getFighterId() + " Buff"
            --+ buff.getModelId() + " 触发属性变化, " + logType +" = "+ attrRatio * plus);
        else
            attrType = convOther(attrType);
            if (needToRatio(attrType)) then
                local attrValue = FighterAttributeManager.getFightAttr(calculator, attrType);
                attrValue = calcRatio(attrValue, attrRatio);
                buff.attributes.changeAttribute(attrType, attrValue * plus);

                --计算一下,自身属性的多少多少作为上限
                local limit = (long)(FighterAttributeManager.getFightAttr(buff.target, attrType) * Utils.toRatio(f_buffEffectValue[i + 2]));
                if (buff.attributes.attributes[attrType] > limit) then
                    --最多只能吸取自身的xxx
                    buff.attributes.attributes[attrType] = limit;
                end

                local logType = attrType;
                local logattrValue = attrValue;
                --LogUtil.info(fight, ()-> "[" + fight.getFrame() + "][EFFECT]Fighter " + buff.target.getFighterId() + " Buff"
                --+ buff.getModelId() + " 触发属性变化, " + logType +" = "+ logattrValue * plus);
            else
                --实际上, 偷别人属性,或别人给自己增加他的xx属性, 是不会走到这个地方来的, 都是走了else 上面的逻辑了
                --因为, 你不可能偷 别人的 n% 的命中, 基本上都是 别人命中 -N   你直接加 N , 因为命中本来就是个百分比的数值类型
                buff.attributes.changeAttribute(attrType, attrRatio * plus);
                local logType = attrType;
                --LogUtil.info(fight, ()-> "[" + fight.getFrame() + "][EFFECT]Fighter " + buff.target.getFighterId() + " Buff"
                --        + buff.getModelId() + " 触发属性变化, " + logType +" = "+ attrRatio * plus);
            end
        end
    end

    ClientFight.BaseBuffEffect.action(self, fight, buff, parameters);

    return IBuffEffect.SUCCESS_ATTRI_HAS_CHANGE;
end


--[[
* 如果来源是自己
* @param attrType
* @return
]]
function t:convSelf(attrType)
    if (attrType == FighterAttributeEnum.HP) then
        return FighterAttributeEnum.HP_PER;
    end
    if (attrType == FighterAttributeEnum.ATK) then
        return FighterAttributeEnum.ATK_PER;
    end
    if (attrType == FighterAttributeEnum.DEF) then
        return FighterAttributeEnum.DEF_PER;
    end
    return attrType;
end

--[[
* 如果来源不是 自己
* @param attrType
* @return
]]
function t:convOther(attrType)
    if (attrType == FighterAttributeEnum.HP_PER) then
        return FighterAttributeEnum.HP;
    end
    if (attrType == FighterAttributeEnum.ATK_PER) then
        return FighterAttributeEnum.ATK;
    end
    if (attrType == FighterAttributeEnum.DEF_PER) then
        return FighterAttributeEnum.DEF;
    end
    return attrType;
end

--[[
* 当来源不是自己时, 是否需要做 百分比 转化
* @param attrType
* @return
]]
function t:needToRatio(attrType)
    return FighterAttributeEnum.HP == attrType or FighterAttributeEnum.ATK == attrType or FighterAttributeEnum.DEF == attrType;
end

t.New()